package com.controller;

import com.entity.Branch;
import com.entity.Emp;
import com.entity.OutStorageDetails;
import com.entity.OutStorageHsy;
import com.service.BranchService;
import com.service.OutStorageDetailsService;
import com.service.OutStorageHsyService;
import com.service.StorageDetailsService;
import com.service.impl.OutStorageDetailsServiceImpl;
import com.service.impl.OutStorageHsyServiceImpl;
import com.until.Result;
import com.vo.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ fileName:OutStorageHsyController
 * @ description:
 * @ author:dzh
 * @ createTime:2022/5/8 14:18
 * @ version:1.0.0
 */
@RestController
@RequestMapping("OutStorageHsy")
public class OutStorageHsyController extends BaseController {
    @Resource
    private OutStorageHsyService outStorageHsyService;
    @Resource
    private OutStorageDetailsService outStorageDetailsService;
    @Resource
    private BranchService branchService;

    @PostMapping("listPass")
    public Result listPass(@RequestBody Page<OutStorageHsy> page) {
        return success(outStorageHsyService.listPass(page));
    }

    @PostMapping("listAudit")
    public Result listAudit(@RequestBody Page<OutStorageHsy> page) {
        return success(outStorageHsyService.listAudit(page));
    }

    @GetMapping("listByShId")
    public Result listByShId(Integer oshId) {
        return success(outStorageDetailsService.listByOshId(oshId));
    }

    @PostMapping("insertOne")
    public Result insertOne(@RequestBody Branch branch, HttpSession session) {
        double amount = branch.getAddStock() * branch.getBook().getOutPrice();
        OutStorageHsy outStorageHsy = new OutStorageHsy(amount, branch.getAddStock(), getSession(session).getEmpName());
        outStorageHsyService.insertOne(outStorageHsy);
        OutStorageDetails outStorageDetails =
                new OutStorageDetails(branch.getBookId(),
                        outStorageHsy.getOshId(),
                        branch.getAddStock(), amount);
        return success(outStorageDetailsService.insertOne(outStorageDetails));
    }

    @PostMapping("insertMore")
    public Result insertMore(@RequestBody List<Branch> branchList, HttpSession session) {
        ArrayList<OutStorageDetails> OutStorageDetails = new ArrayList<>();
        Emp emp = getSession(session);
        double amount = 0;
        int sum = 0;
        for (Branch branch : branchList) {
            amount += branch.getAddStock() * branch.getBook().getOutPrice();
            sum += branch.getAddStock();
        }
        OutStorageHsy outStorageHsy = new OutStorageHsy(amount, sum, emp.getEmpName());
        outStorageHsyService.insertMore(outStorageHsy);
        for (Branch branch : branchList) {
            OutStorageDetails outStorageDetails =
                    new OutStorageDetails(branch.getBookId(),
                            outStorageHsy.getOshId(),
                            branch.getAddStock(),
                            branch.getAddStock() * branch.getBook().getOutPrice());
            OutStorageDetails.add(outStorageDetails);
        }
        return success(outStorageDetailsService.insertMore(OutStorageDetails));
    }

    @PostMapping("updatePass")
    public Result updatePass(@RequestBody OutStorageHsy outStorageHsy,HttpSession session) {
        Emp emp = getSession(session);
        List<OutStorageDetails> outStorageDetails = outStorageDetailsService.listByOshId(outStorageHsy.getOshId());
        for (OutStorageDetails outStorageDetail : outStorageDetails) {
            Branch branch = branchService.listById(emp.getStoresId(),outStorageDetail.getBookId());
            branchService.update(new Branch(emp.getStoresId(), outStorageDetail.getBookId(),
                    branch.getBookQty()-outStorageDetail.getOutStorageQty()));
        }
        outStorageHsy.setAuditor(emp.getEmpName());
        return success(outStorageHsyService.updatePass(outStorageHsy));
    }
    @PostMapping("updateReject")
    public Result updateReject(@RequestBody OutStorageHsy outStorageHsy,HttpSession session) {
        outStorageHsy.setAuditor(getSession(session).getEmpName());
        return success(outStorageHsyService.updateReject(outStorageHsy));
    }

}
